package com.financing.wap.web.controller.api;

import java.io.IOException;
import java.lang.annotation.Documented;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;

import org.apache.log4j.Logger;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.financing.wap.common.ResultMap;
import com.financing.wap.common.ResultMap.STATUS;
import com.financing.wap.util.MemcacheCacheHelper;

@Controller("countAnythingController")
@RequestMapping("/api/count")
public class CountAnythingController {
	
	Logger logger = Logger.getLogger(getClass());
	
	/**
	 *  type 为1：展示统计信息
	 * type为 2：增加统计信息
	 * @param request
	 * @param type
	 * @param productName
	 * @param productOperation
	 * @param response
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "/hybxCount")
	@ResponseBody
	public ResultMap hybxCount(HttpServletRequest request,String type, String productName , String productOperation,
			HttpServletResponse response) throws IOException {
		
		ResultMap result = new ResultMap();
		String ref = "hybxCount";
		try{
						if("1".equals(type)){
							// 展示统计信息
							String[] productOperatinos = productOperation.split(",");
							for(String s:productOperatinos){
								Object count = MemcacheCacheHelper.mcc.get(ref+productName+s);
								if(null == count ){
									count  = new Integer(0);
								}
								result.setAttribute(s, (Integer)count);
							}
							
						}
						if("2".equals(type)){
							// 增加统计信息
							Object count = MemcacheCacheHelper.mcc.get(ref+productName+productOperation);
							if(null == count || (! (count instanceof Integer))){
								count =Integer.valueOf(0);
							}
							int icount = ((Integer)count).intValue();
							MemcacheCacheHelper.mcc.set(ref+productName+productOperation,Integer.valueOf(++icount));
							result.setAttribute(productOperation, Integer.valueOf(icount));
						}
		
				result.setStatus(STATUS.SUCCESS);
				result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
		}catch(Exception e){
			logger.error("count hybx error:",e);
			result.setStatus(STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		return  result;
			
	}
}
